package com.yangcangclub.commons.utils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;

/**
 * 不要用DES加解密。
 *
 */
public class AES {
    //永远不变
    public static final String KEY="A3DWan6NP84sdl4zj8D2wiF858pdsBG64Ddsmx5q1D0xwd8";
    /**
     * 加密
     *
     * @param content
     *            待加密内容
     * @return
     */
    public static String encrypt(String content)throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom random=SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(KEY.getBytes());
        kgen.init(128, random);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        byte[] byteContent = content.getBytes("utf-8");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] byteRresult = cipher.doFinal(byteContent);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteRresult.length; i++) {
            String hex = Integer.toHexString(byteRresult[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 解密
     * @param content
     *            待解密内容
     * @return
     */
    public static String decrypt(String content){
        if (content.length() < 1)
            return null;
        byte[] result;
        try {
            byte[] byteRresult = new byte[content.length() / 2];
            for (int i = 0; i < content.length() / 2; i++) {
                int high = Integer.parseInt(content.substring(i * 2, i * 2 + 1),16);
                int low = Integer.parseInt(content.substring(i * 2 + 1, i * 2 + 2),16);
                byteRresult[i] = (byte) (high * 16 + low);
            }
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom random=SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(KEY.getBytes());
            kgen.init(128, random);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            result = cipher.doFinal(byteRresult);
            return new String(result);
        } catch (Exception e) {
            System.out.println(" AES  decrypt  Exetption");
        }
        return "";
    }




    public static void main(String[] args) {
        try {
            String content = "192.168.1.2"+"^"+StringUtil.getRandomStr(10);
            //加密   
            System.out.println("加密前：" + content);
            String str=encrypt(content);
            System.out.println("加密后：" +str);
            //解密   
            String str2 = decrypt(str);
            System.out.println("解密后：" + str2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
